package com.atguigu.eduOrder.controller;


import com.atguigu.common.Result;
import com.atguigu.eduOrder.entity.Order;
import com.atguigu.eduOrder.service.OrderService;
import com.atguigu.eduOrder.service.PayLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/eduOrder/payLog")
public class PayLogController {

    @Resource
    private PayLogService payLogService;

    @ApiOperation("根据订单号生成二维码")
    @GetMapping("/createWx/{orderNo}")
    public Result createWx(@PathVariable String orderNo){

        //返回信息,包含二维码和其他信息
        Map<String,Object> map = payLogService.createWx(orderNo);
        System.out.println(map);
        return Result.ok().data(map);
    }

    @ApiOperation("根据订单号查询订单状态")
    @GetMapping("/payStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo){

        Map<String ,String> map = payLogService.queryPayStatus(orderNo);
        System.out.println(map);
        if(map == null){
            return Result.error().message("支付失败");
        }
        if("SUCCESS".equals(map.get("trade_state"))){
            //添加记录到支付表,更新订单表状态
            payLogService.updateOrderStatus(map);
            return Result.ok().message("success");
        }
        return Result.ok().code(250).message("支付中");

    }

}

